package cn.okcode.slowjson.ast;

import cn.okcode.slowjson.parser.Visitor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author pyk13
 */
public class ObjectNode extends AbstractNode<Map<PropertyNode, Object>> {
    private final Map<PropertyNode, AbstractNode<?>> properties;
    private final Map<String, AbstractNode<?>> propertyNodes;

    public Map<PropertyNode, AbstractNode<?>> getProperties() {
        return properties;
    }

    public AbstractNode<?> getPropertyValueNode(String propertyName) {
        return propertyNodes.get(propertyName);
    }


    public ObjectNode(Map<PropertyNode, AbstractNode<?>> properties) {
        this.properties = properties;
        this.propertyNodes = new HashMap<>(properties.size());
        for (Map.Entry<PropertyNode, AbstractNode<?>> entry : properties.entrySet()) {
            entry.getValue().parent = this;
            this.propertyNodes.put(entry.getKey().getValue(), entry.getValue());
        }
    }

    @Override
    public Map<PropertyNode, Object> getValue() {
        throw new UnsupportedOperationException();
    }

    @Override
    public String stringValue() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
